package com.jingdianjichi.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.application.Dto.SubjectCategoryDTO;
import com.jingdianjichi.application.convent.SubjectCategoryDTOConventer;
import com.jingdianjichi.subject.common.entity.Result;

import com.jingdianjichi.subject.domain.entity.SubjectCategoryBo;
import com.jingdianjichi.subject.domain.service.SubjectCategoryDomainService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/subject/category")
@Slf4j

public class SubjectCategoryControlller {

    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    @PostMapping("/add")

    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.add.dto,{}", JSON.toJSONString(subjectCategoryDTO));
            }

            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryName(), "分类名称不能为空");
            Preconditions.checkArgument(!StringUtils.isEmpty(subjectCategoryDTO.getCategoryType()), "分类类型不能为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "分类父ID不能为空");


            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDTOConventer.INSERT.conventBoToCategory(subjectCategoryDTO);
            subjectCategoryDomainService.add(subjectCategoryBo);
            return Result.ok();
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error,{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }


    @PostMapping("/queryPrimaryCategory")

    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {

        try {

            SubjectCategoryBo subjectCategoryBo = new SubjectCategoryBo();
            BeanUtils.copyProperties(subjectCategoryDTO, subjectCategoryBo);


            List<SubjectCategoryBo> boList = subjectCategoryDomainService.queryCategoryByPrimary(subjectCategoryBo);
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.queryPrimaryCategory,{}", JSON.toJSONString(boList));
            }


            List<SubjectCategoryDTO> subjectCategoryDTOS = SubjectCategoryDTOConventer.INSERT.conventToDTOCategory(boList);
            return Result.ok(subjectCategoryDTOS);

        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error,{}", e.getMessage(), e);
            return Result.fail("查询失败");
        }
    }


    @PostMapping("/queryCategoryByPrimary")

    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {

        try {

            SubjectCategoryBo subjectCategoryBo = new SubjectCategoryBo();
            BeanUtils.copyProperties(subjectCategoryDTO, subjectCategoryBo);
            Preconditions.checkNotNull(subjectCategoryBo.getParentId(), "父级ID不能为空");

            List<SubjectCategoryBo> boList = subjectCategoryDomainService.queryCategoryByPrimary(subjectCategoryBo);
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.queryPrimaryCategory,{}", JSON.toJSONString(boList));
            }


            List<SubjectCategoryDTO> subjectCategoryDTOS = SubjectCategoryDTOConventer.INSERT.conventToDTOCategory(boList);
            return Result.ok(subjectCategoryDTOS);




        } catch (Exception e) {
            log.error("SubjectCategoryController.quary.error,{}", e.getMessage(), e);
            return Result.fail("查询失败");
        }
    }


    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {


        try {
            SubjectCategoryBo subjectCategoryBo = new SubjectCategoryBo();
            BeanUtils.copyProperties(subjectCategoryDTO, subjectCategoryBo);
            Preconditions.checkNotNull(subjectCategoryBo.getId(), "分类ID不能为空");

            Boolean result = subjectCategoryDomainService.update(subjectCategoryBo);
            return Result.ok(result);

        } catch (Exception e) {
            log.error("SubjectCategoryController.update.error,{}", e.getMessage(), e);
            return Result.fail("更新失败");

        }


    }


    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {


        try {
            SubjectCategoryBo subjectCategoryBo = new SubjectCategoryBo();
            BeanUtils.copyProperties(subjectCategoryDTO, subjectCategoryBo);
            Preconditions.checkNotNull(subjectCategoryBo.getId(), "分类ID不能为空");
            Boolean b = subjectCategoryDomainService.delect(subjectCategoryBo);
            return Result.ok(b);

        } catch (Exception e) {
            log.error("SubjectCategoryController.delete.error,{}", e.getMessage(), e);
            return Result.fail("删除失败");
        }
    }
}
